package com.eascs.spider.dsk.spider;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.eascs.spider.dsk.db.MongoSession;
import com.eascs.spider.dsk.db.RedisSession;
import com.eascs.spider.dsk.downloader.Downloader;
import com.eascs.spider.dsk.entity.StaticParam;

/**
 * 鼎搜客公司列表搜集
 * @author:hzp
 * @date:2018年4月17日
*/
public class DskCompanyListSpider implements Runnable{

	private static Logger logger = LoggerFactory.getLogger(DskCompanyListSpider.class);
	
	private JSONObject param ;
	
	private int failureCount = 0;//单个线程获取公司列表信息失败次数，包括返回列表为空或异常情况
	
	private int pageIndex ;
	
	private MongoSession mongoSession ;
	
	private RedisSession redisSession;
	
	private String paramFlag ;//任务条件标识内容，param.getString("Brand")+"|"+param.getString("Channel")
	
	public DskCompanyListSpider(JSONObject param) {
		this.param = param;
		this.pageIndex = Integer.valueOf(param.getString("pageIndex"));
		this.paramFlag = param.getString("Brand")+"|"+param.getString("Channel");
		this.mongoSession = MongoSession.getInstance();
		this.redisSession = RedisSession.getIntance();
		
	}
	
	@Override
	public void run(){
		while(failureCount < 3){
			param.put("PageIndex", ""+pageIndex);
			logger.info("请求参数：{} ,第{}页" , paramFlag ,pageIndex);
			List<Map<String , Object>> businessList = getBusinessesList(param.toString());
			if( businessList == null || businessList.size() ==0){
				logger.warn("请求参数：{} ,失败次数加1 ,failureCount:{}" ,paramFlag ,failureCount);
				continue;
			}
			for(Map<String, Object> company : businessList){
				if(redisSession.sexists(StaticParam.DSK_REDIS_QUERY_LIST_QUCHONG, ""+company.get("fcustid")))
						continue;
				company.put("Brand", param.getString("Brand"));
				company.put("Channel", param.getString("Channel"));
				logger.info("公司列表基本信息:{}" ,JSON.toJSONString(company));
				mongoSession.set(StaticParam.DSK_DB_COLLECTION_BASIC, company);
				redisSession.sadd(StaticParam.DSK_REDIS_QUERY_LIST_QUCHONG, ""+company.get("fcustid"));
				redisSession.lpush(StaticParam.DSK_REDIS_COMPANY_QUEUE, company.get("fcustid") + "|" + company.get("fcustname"));
			}
			//刷新缓存对应采集条件的状态
			redisSession.hset(StaticParam.DSK_REDIS_QUERYPARAM_CACHE, paramFlag , ""+pageIndex);
			pageIndex++;
			sleep();
		}
		logger.info("任务条件：{}已完成" ,paramFlag);
		redisSession.hset(StaticParam.DSK_REDIS_QUERYPARAM_CACHE, paramFlag, StaticParam.TASK_REDIS_FINISHED);
	}
	
	public List<Map<String, Object>> getBusinessesList(String paramterdata){
		String url = "http://dingmou.net:8019/dmsock/company/getCompanylist";
		Map<String, String> param = new HashMap<>();
		param.put("parameterdata", paramterdata);
		Downloader downloader = Downloader.build()
										.setUrl(url)
										.setParam(param)
										.setHttpMethod("POST")
										.download();
		String html = downloader.getHtml();
		List<Map<String, Object>> list = new LinkedList<>();
		try {
			JSONObject jsonObject = new JSONObject(html);
			if(html == null || !jsonObject.has("meta") || jsonObject.getJSONObject("meta").getInt("code") != 200){
				logger.error("获取商家列表信息出错,参数:{},html:{}" ,param.toString() , html);
				failureCount++;
				return null;
			}
			JSONArray array = jsonObject.getJSONArray("data");
			for(int i=0;i<array.length();i++){
				JSONObject json = array.getJSONObject(i);
				Map<String, Object> data = new HashMap<>();
				for(Object key :json.keySet()){
					data.put((String)key, json.get((String)key));
				}
				list.add(data);
			}	
		} catch (Exception e) {
			logger.error("解析异常,html:{}" ,html , e);
		}
		if(list.size() == 0)
			failureCount++;
		return list;
	}	
	
	//随机休息20-25秒
	private void sleep(){
		try {
			int time = (int)(Math.random()*6+20);
			Thread.sleep(time*1000L);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
